name: tests

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

env:
  IMAGE_SHARD: spqr-shard-image
  SHARD_CACHE_KEY: sha256sum-of-docker-shard
  CACHE_FILE_SHARD: ~/spqr-shard-image-shard-cache-key.tgz

jobs:

  unit:
     name: unit
     runs-on: ubuntu-latest
     steps:

     - name: Check out code
       uses: actions/checkout@v4

     - name: unit tests
       run: make unittest

  stress:
    name: stress
    runs-on: ubuntu-latest
    steps:

      - name: Check out code
        uses: actions/checkout@v4

      - name: stress tests
        run: make stress
  regress:
    name: regress
    runs-on: ubuntu-latest
    steps:

      - name: Check out code
        uses: actions/checkout@v4

      - name: regress tests
        run: |
          POSTGRES_VERSION=12 make regress
          POSTGRES_VERSION=13 make regress
          POSTGRES_VERSION=14 make regress
          POSTGRES_VERSION=15 make regress

  gorm-regress:
    name: drivers_gorm
    runs-on: ubuntu-latest
    steps:

      - name: Check out code
        uses: actions/checkout@v4

      - name: Run tests
        run: make gorm_regress

  xproto-regress:
    name: xproto
    runs-on: ubuntu-latest
    steps:

      - name: Check out code
        uses: actions/checkout@v4

      - name: Run tests
        run: make xproto_regress

  feature_prepare:
    name: feature_prepare
    runs-on: ubuntu-latest
    outputs:
      output1: ${{ steps.step1.outputs.features }}
    steps:
      - name: Check Docker Version
        run: docker --version

      - name: Check out code
        uses: actions/checkout@v4

      - id: step1
        run: |
          make split_feature_test
          array=$(find test/feature/generatedFeatures -type f -exec wc -l {} \; | sort -rn | awk '{printf $0"\n"}' | python3 .github/scenario_balancer.py)
          echo "features=$(echo "{\"command\": $array}")" >> $GITHUB_OUTPUT

      - name: Archive production artifacts
        uses: actions/upload-artifact@v4
        with:
          name: generated_tests
          path: |
            ./test/feature/generatedFeatures

      - name: Update env
        run: |
          SHARD_CACHE_KEY="spqr-shard-image-$(git rev-parse HEAD)"
          CACHE_FILE_SHARD="~/$SHARD_CACHE_KEY.tgz"
          echo "SHARD_CACHE_KEY=$SHARD_CACHE_KEY" >> $GITHUB_ENV
          echo "CACHE_FILE_SHARD=$CACHE_FILE_SHARD" >> $GITHUB_ENV

      #  use cache to pass docker images to the test jobs
      - name: Docker shard image caching
        id: cache-shard-image
        uses: actions/cache@v4
        with:
          path: |
            ${{ env.CACHE_FILE_SHARD }}
          key: ${{ env.SHARD_CACHE_KEY }}

      # build images
      - name: Build shard image
        if: steps.cache-shard-image.outputs.cache-hit != 'true'
        run: make save_shard_image


  feature:
    name: feature
    runs-on: ubuntu-latest
    needs: feature_prepare
    strategy:
      matrix: ${{ fromJson(needs.feature_prepare.outputs.output1) }}
      # do not cancel all tests if one failed
      fail-fast: false
    steps:
      - name: Install Latest Docker
        run: |
          for pkg in docker.io docker-doc podman-docker containerd runc; do sudo apt-get remove $pkg; done
          curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
          sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu  $(lsb_release -cs)  stable"
          sudo apt-get update
          sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
      - name: Check Docker Version
        run: docker --version
      - name: Check out code
        uses: actions/checkout@v4

      - name: Update env
        run: |
          SHARD_CACHE_KEY="spqr-shard-image-$(git rev-parse HEAD)"
          CACHE_FILE_SHARD="~/$SHARD_CACHE_KEY.tgz"
          echo "SHARD_CACHE_KEY=$SHARD_CACHE_KEY" >> $GITHUB_ENV
          echo "CACHE_FILE_SHARD=$CACHE_FILE_SHARD" >> $GITHUB_ENV

      - name: Download generatedFeatures
        uses: actions/download-artifact@v4
        with:
          name: generated_tests
          path: ./test/feature/generatedFeatures

      # load docker images
      - name: Load shard image
        id: cache-shard-image
        uses: actions/cache@v4
        with:
          path: |
            ${{ env.CACHE_FILE_SHARD }}
          key: ${{ env.SHARD_CACHE_KEY }}

      - name: feature tests
        run: ${{ matrix.command }}
